package com.arrogames.rgblabyrinth.menu {
	import com.arrogames.rgblabyrinth.AssetManager;
	import com.arrogames.rgblabyrinth.menu.components.ColorPicker;
	import com.arrogames.rgblabyrinth.menu.events.StartGameEvent;
	import com.arrogames.util.UIUtil;
	
	import feathers.controls.Button;
	import feathers.controls.Label;
	import feathers.controls.Screen;
	
	import starling.display.DisplayObject;
	import starling.events.Event;
	
	public class NewGameScreen extends Screen {
		private var currentColor:uint;
		public function NewGameScreen() {
			super();
		}
		
		override protected function initialize():void {
			super.initialize();
			
			addChild(createTitleDisplay());
			addChild(createColorPicker());
			addChild(UIUtil.createButton(AssetManager.instance.getTexture("newgameBtn"), 800-128-20, 600-128-20, onConfirmClick));
			addChild(UIUtil.createButton(AssetManager.instance.getTexture("backBtn"), 20, 600-128-20, onBackClick));
		}
		
		private function createTitleDisplay():DisplayObject {
			var label:Label = new Label();
			label.text = "Select a color:";
			label.x = 200;
			label.y = 50;
			return label;
		}
		
		private function createColorPicker():DisplayObject {
			var result:ColorPicker = new ColorPicker();
			result.addEventListener(ColorPicker.COLOR_CHANGED, onColorSelect);
			result.x = 400;
			result.y = 300;
			return result;
		}
		
		private function onColorSelect(event:Event):void {
			var cp:ColorPicker = event.currentTarget as ColorPicker;
			currentColor = cp.selectedColor;
		}
		
		private function createConfirmButton():DisplayObject {
			var button:Button = new Button();
			button.label = "Start!";
			button.x = 600;
			button.y = 550;
			button.addEventListener(Event.TRIGGERED, onConfirmClick);
			return button;
		}
		
		private function onConfirmClick(event:Event):void {
			var button:Button = event.currentTarget as Button;
			dispatchEvent(new StartGameEvent(StartGameEvent.NEWGAME, currentColor));
		}
		
		private function onBackClick(event:Event):void {
			var button:Button = event.currentTarget as Button;
			this.owner.showScreen(RGBNavigator.MAINMENU);
			
		}
	}
}